package ar.com.angrymachine.hunter.locations;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.GameSettings;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.HunterResourceManager;

public class ColossusHeadActivity extends Activity
{
	ColossusActivitySet parentActivitySet;
	
	int shakeTime = 0;
	int graspTime = GameSettings.COLOSSUS_GRASP_TIME;
	
	public ColossusHeadActivity(ColossusActivitySet parentSet)
	{
		parentActivitySet = parentSet;
	}
	
	private boolean isShaking()
	{
		return parentActivitySet.shaking;
	}
	
	public String getActionText()
	{
		return Locale.CLING;
	}

	public String getDescription()
	{
		return Locale.colossusLimbDescription(Locale.COLOSSUS_BODY, isShaking());
	}

	public void paint(Graphics g, Rectangle activityArea)
	{
        GraphicStyles.paintDefaultActivity(g, this, activityArea, HunterResourceManager.getImage("town-arena.png"));
        if(!isCompleted())
        {
        	Rectangle[] bars = GraphicStyles.getBarAreas(activityArea, 2);
        	// shake
            g.setColor(Color.YELLOW);
            String shakeString = isShaking()? Locale.SHAKING : Locale.ABOUT_TO_SHAKE;
            GraphicStyles.paintLabeledBar(g, shakeString, bars[0], shakeTime, GameSettings.COLOSSUS_SHAKE_TIME);
            if(isShaking())
            {
	            // grasp
	            g.setColor(Color.BLUE);
	            GraphicStyles.paintLabeledBar(g, Locale.GRASP, bars[1], graspTime, GameSettings.COLOSSUS_GRASP_TIME);
            }
        }
	}

	public void update(int delta, boolean action, boolean timeIsRunning)
	{
		if(timeIsRunning)
		{
			if(isShaking())
			{
				shakeTime -= delta;
				if(shakeTime <= 0)
				{
					shakeTime = 0;
					// colossus stopped shaking
					parentActivitySet.shaking = false;
					graspTime = GameSettings.COLOSSUS_GRASP_TIME;
				}
				if(!action)
				{
					graspTime -= delta;
					graspTime = Math.max(0, graspTime);
					if(graspTime == 0)
					{
						// grasp lost
						parentActivitySet.onGraspLost();
					}
				}
			}
			else
			{
				shakeTime += delta;
				if(shakeTime >= GameSettings.COLOSSUS_SHAKE_TIME)
				{
					shakeTime = GameSettings.COLOSSUS_SHAKE_TIME;
					// colossus starts shaking!
					parentActivitySet.shaking = true;
				}				
			}
		}
	}
}
